﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToe
{
    class TicTacToe
    {
        private int cursorx = 0;
        private int cursory = 0;
        private int playerturn = 1;
        private char[,] board = new char [,] { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };

        public void Draw()
        {
            Console.Clear();
            for (int i = 0; i < board.GetLength(0); i++)
            {
                Console.WriteLine("-------");
                for (int j = 0; j < board.GetLength(1); j++)
                {
                    Console.Write("|" + board[i,j]);
                }
                Console.Write("|\n");
                
            }
            Console.WriteLine("-------");

            Console.SetCursorPosition(0, 10);
            Console.WriteLine("Current Player:");
            Console.WriteLine("Player " + playerturn);

            //show cursor
            Console.SetCursorPosition(cursorx * 2 + 1, cursory * 2 + 1);
        }

        public void Play()
        {
            Console.CursorSize = 1;
            bool winner = false;
            while (!winner)
            {       
                //get input
                System.ConsoleKeyInfo keypress = Console.ReadKey();
                switch (keypress.Key)
                {
                    case System.ConsoleKey.Enter:
                        switch (playerturn)
                        {
                            case 1:
                                if (board[cursory, cursorx] == ' ')
                                {
                                    board[cursory, cursorx] = 'x';
                                    winner = CheckWin();
                                    playerturn = 2;
                                    //draw();
                                }
                                break;
                            case 2:
                                if (board[cursory, cursorx] == ' ')
                                {
                                    board[cursory, cursorx] = 'o';
                                    winner = CheckWin();
                                    playerturn = 1;
                                    //draw();
                                }
                                break;
                            default:
                                break;
                        }
                        break;
                    case System.ConsoleKey.UpArrow:
                        if (cursory > 0)
                        {
                            cursory--;
                        }
                        break;
                    case System.ConsoleKey.DownArrow:
                        if (cursory < board.GetLength(1) - 1)
                        {
                            cursory++;
                        }
                        break;
                    case System.ConsoleKey.LeftArrow:
                        if (cursorx > 0)
                        {
                            cursorx--;
                        }
                        break;
                    case System.ConsoleKey.RightArrow:
                        if (cursorx < board.GetLength(0) - 1)
                        {
                            cursorx++;
                        }
                        break;
                    default:
                        break;
                }
                Draw();     
            }
        }

        private bool CheckWin()
        {
            for(int i = 0; i < board.GetLength(0); i++){
                if (board[i,0] != ' ' && board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2])
                {
                    return true;
                }
                else if (board[0, i] != ' ' && board[0, i] == board[1, i] && board[1, i] == board[2, i])
                {
                    return true;
                }
            }
            if (board[0, 0] != ' ' && board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2])
            {
                return true;
            }
            else if (board[2, 0] != ' ' && board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2])
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
